﻿<?php

class PromocoesModel 
{
    function __construct($db) {
        try {
            $this->db = $db;
        } catch (PDOException $e) {
            exit('Database connection could not be established.');
        }
    }
	
	public function adiciona($valores) 
	{    		
        $sql = "INSERT INTO promocao (nome, preco, desconto, descricao, foto, datainicio, datafim, loja, id_usuario) VALUES (:nome, :preco, :desconto, :descricao, :foto, :datainicio, :datafim, :loja, :id_usuario)";
        $query = $this->db->prepare($sql);
        $query->execute(array(
			':nome' => $valores['nome'], 
			':preco' => $valores['preco'],
			':desconto' => $valores['desconto'], 
			':descricao' => $valores['descricao'],
			':foto' => $valores['foto'],
			':datainicio' => $valores['datainicio'],
			':datafim' => $valores['datafim'],
			':loja' => $valores['loja'],
			':id_usuario' => $valores['id_usuario']
		));
    }
	
	public function consulta()
	{
		$sql = "SELECT id, nome, preco, desconto, descricao, foto, datainicio, datafim, loja, id_usuario FROM promocao WHERE datafim > NOW()";
		$query = $this->db->prepare($sql);
		$query->execute();
		return $query->fetchAll();
	}

	public function consultaPaginaInicial()
	{
		$sql = "SELECT * FROM promocao WHERE datafim > NOW() LIMIT 4";
		$query = $this->db->prepare($sql);
		$query->execute();
		return $query->fetchAll();
	}

	public function porId($id)
	{
		$sql = "SELECT promocao.*, DATEDIFF(promocao.datafim,NOW()) AS diasRestantes FROM promocao WHERE id = :id AND datafim > NOW() ";
		$query = $this->db->prepare($sql);
		$query->execute(array(":id" => $id));
		return $query->fetch();
	}

	public function consultaMinhasPromocoes($id)
	{
		$sql = "SELECT promocao.*, DATEDIFF(promocao.datafim,NOW()) AS diasRestantes FROM promocao WHERE id_usuario = :id ORDER BY datafim ASC";
		$query = $this->db->prepare($sql);
		$query->execute(array(':id' => $id));
		return $query->fetchAll();
	}

	public function cancelarMinhaPromocao($id_usuario, $id)
	{
		$sql = "UPDATE promocao SET datafim = NOW() WHERE id_usuario = :id_usuario AND id = :id";
		$query = $this->db->prepare($sql);
		$query->execute(array(':id_usuario' => $id_usuario, ':id' => $id));
	}

	public function busca($busca)
	{
		$sql = "SELECT * FROM promocao WHERE ((nome LIKE '%".$busca."%') OR (preco LIKE '%".$busca."%') OR (desconto LIKE '%".$busca."%') OR (descricao LIKE '%".$busca."%') OR (loja LIKE '%".$busca."%') OR ('%".$busca."%')) and datafim > NOW() ORDER BY desconto DESC";
		$query = $this->db->prepare($sql);
		$query->execute();
		return $query->fetchAll();	
	}

	public function cancelarPromocao($id)
	{
		$sql = "UPDATE promocao SET datafim = NOW() WHERE id = :id";
		$query = $this->db->prepare($sql);
		$query->execute(array(':id' => $id));
	}

	public function deletarPromocao($id)
	{
		$sql = "DELETE FROM promocao WHERE id = :id";
		$query = $this->db->prepare($sql);
		$query->execute(array(':id' => $id));
	}


}